﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Device;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Device
{
    /// <summary>
    /// 设备台账业务逻辑层
    /// </summary>
    public class MachineryServices:IMachineryServices
    {
        public MachineryServices(IBaseRepository db) 
        {
            DB = db;
        }
        private IBaseRepository DB { get;}
        public async Task<bool> AddAsync(Machinery model)
        {
            var result = await DB.InsertAsync(model);
            return result;
        }
        public async Task<bool> DeleteAsync(int[] Ids)
        {

            string sql = "delete Machinery where Id in @Ids";
            var result = await DB.DeleteAsync(sql, new { Ids });
            return result;
        }
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Machinery where Id = @Id";
            var result = await DB.DeleteAsync(sql, new { Id });
            return result;
        }
        public async Task<IEnumerable<Machinery>> GetAsync(int PageIndex, int PageSize, string MachineryTypeName, string MachineryCode, string MachineryName, string Status, int MachineryTypeId)
        {
            string sql = "select * from Machinery where 1=1 ";
            if (!string.IsNullOrEmpty(MachineryTypeName))
            {
                sql += " and MachineryTypeName like @MachineryTypeName ";
            }
            if (!string.IsNullOrEmpty(MachineryCode))
            {
                sql += " and MachineryCode like @MachineryCode ";
            }
            if (!string.IsNullOrEmpty(MachineryName)) 
            {
                sql += " and MachineryName like @MachineryName ";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }
            if (MachineryTypeId != -1)
            {
                sql += " and MachineryTypeId  = @MachineryTypeId ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await DB.QueryAsync<Machinery>(sql, new { PageIndex, PageSize, MachineryTypeName = $"%{MachineryTypeName}%", MachineryCode = $"%{MachineryCode}%", MachineryName=$"%{MachineryName}%", Status=$"%{Status}%", MachineryTypeId });
            return list;
        }
        public async Task<IEnumerable<Machinery>> GetAllAsync()
        {
            string sql = "select * from Machinery";
            var list = await DB.QueryAllAsync<Machinery>(sql);
            return list;
        }
        public async Task<bool> UpdateAsync(Machinery model)
        {
            var result = await DB.UpdateAsync(model);
            return result;
        }
        public async Task<int> GetCountAsync(string MachineryTypeName, string MachineryCode, string MachineryName, string Status,int MachineryTypeId)
        {
            string sql = "select count(*) from Machinery where 1=1 ";
            if (!string.IsNullOrEmpty(MachineryTypeName))
            {
                sql += " and MachineryTypeName like @MachineryTypeName ";
            }
            if (!string.IsNullOrEmpty(MachineryCode))
            {
                sql += " and MachineryCode like @MachineryCode ";
            }
            if (!string.IsNullOrEmpty(MachineryName))
            {
                sql += " and MachineryName like @MachineryName ";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }
            if (MachineryTypeId != -1)
            {
                sql += " and MachineryTypeId  = @MachineryTypeId ";
            }
            var count = await DB.GetRecoreCountAsync(sql, new { MachineryTypeName = $"%{MachineryTypeName}%", MachineryCode = $"%{MachineryCode}%", MachineryName = $"%{MachineryName}%", Status = $"%{Status}%", MachineryTypeId });
            return count;
        }
    }
}
